<?php
/**
 * 金币记录
 */

namespace app\api\model;
use think\Model;
use think\Db;

class GoldRecord extends Model
{
   
    /**
    * 列表
    */
    public function _list($where, $order_by="created asc", $page_size = PAGE_SIZE_API){
        $obj = $this->order($order_by);
        $data = $this->where($where)
            ->field('id, type_id,  remark, gold, created')
            ->order($order_by)
            ->paginate($page_size)
            ->toArray();
        return $data;
    }
    
    /**
    * 领取金币每日金币
    * @param mid int 会员id
    * @param udid string 会员udid
    * @param level_id int 会员等级id
    */
    public function  _receiveOneDayGold($mid, $udid, $level_id){
        $model = new MemberLevel;
        $ret = $model->_getById($level_id);
        if(!$ret){
            $this->error = $this->getError();
            return false;
        }
        
        $gold = $ret['gold'];
        $type_id = 4;
        $remark = '系统赠送金币';
        $ip = ip2long(get_ip());
        
        $last = $this->where([['mid', '=', $mid], ['type_id', '=', 4]])->order('created desc')->find();
        //var_dump($gold);die;
        if(!empty($last)){
            $lastTime = $last['created'];
            $lastDate = strtotime(date("Y-m-d", $lastTime));
            $currentDate = strtotime(date("Y-m-d"));
            
            if($currentDate == $lastDate){
                $this->error = '已领取，请明日再来';
                return false;
            }
        }
        
        if($gold > 0){
            $this->startTrans();
            $data = [
                'mid' => $mid,
                'udid' => $udid,
                'type_id' => $type_id,
                'serial_number' => serialNumber(),
                'gold' => $gold,
                'remark' => $remark,
                'ip' => $ip,
                'created' => time(),
            ];
            
            $ret = $this->save($data);
            
            if(!$ret){
                $this->error = '领取失败';
                return false;
            }
            $sql = "update sy_member set gold=gold+$gold where id = $mid and status = 1 and is_del = -1 and $gold > 0";
            $ret = Db::execute($sql);
            if(!$ret){
                $this->error = '领取失败';
                return false;
            }
            $sql = "update sy_member_ingo set total_gold = total_gold + $gold where mid = $mid and $gold > 0";
            if(!$ret){
                $this->error = '领取失败';
                return false;
            }
            
        }
        $this->commit();
        return true;
    }
    
    /**
    * 校验今日是否领取过该类型金币金币
    * @param mid int 会员id
    * @param type_id int 金币类型id
    * @return bool true：未领取 flase：已领取
    */
    public function _isReceive($mid, $type_id){
        $last = $this->where([['mid', '=', $mid], ['type_id', '=', $type_id]])->order('created desc')->find();
        if(!empty($last)){
            $lastTime = $last['created'];
            $lastDate = strtotime(date("Y-m-d", $lastTime));
            $currentDate = strtotime(date("Y-m-d"));
            
            if($currentDate == $lastDate){
                $this->error = '已领取，请明日再来';
                return false;
            }
        }
        return true;
    }
    
    
    /**
    * 今日金币
    * @param mid 会员id
    * @param udid 设备udid
    * @param isMember 是否是会员
    * @return float 今日金币
    */
    public function _todayGold($mid, $udid, $isMember){
        $gold = 0;
        
        $start = strtotime(date("Y-m-d"));
        $end = strtotime(date("Y-m-d")) + 24 * 3600;
        if($isMember){
            $where = [
                ['mid', '=', $mid],
                ['created', '>=', $start],
                ['created', '<', $end],
            ];
            $gold = $this->where($where)->sum('gold');
        }else{
            $where = [
                ['mid', '=', 0],
                ['udid', '=', $udid],
                ['is_tourist', '=', 1],
                ['created', '>=', $start],
                ['created', '<', $end],
            ];
            $gold = $this->where($where)->sum('gold');
        }
        
        return $gold;
    }
    
}